// (c) Copyright Microsoft Corporation.
// this.source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


Type.registerNamespace('GridControl');



GridControl.TabControlBehavior = function(element) {
    GridControl.TabControlBehavior.initializeBase(this, [element]);
 
}
GridControl.TabControlBehavior.prototype = {
    
    // The initialize function is responsible for getting the selected index from
    // the ClientState mechanism and walking the children of the behavior's target
    // to find all of the GridControl's child panes.  It builds up collections of the
    // headers and the content panes as well as wires up the headers to an event
    // handler.  Finally, all the content panes except the selected one are hidden.
    initialize : function() {
        GridControl.TabControlBehavior.callBaseMethod(this, 'initialize');
        
        
        
        var id=this._id.replace("_TabControlExtender","");
        //create the Tab Control
        var jtabs = new Ext.TabPanel(id);
        this._tab=jtabs;
       
        var e = this.get_element();
        var children = e.childNodes;
        var active=false;
        var arrTabs= new Array();
        for (var i = 0;i < children.length; i++) {
            var child = children[i];
            //put the tabs
            if (child.className=="tab-content") {
                 arrTabs.push(child.id);
            }
        }
        
       
        for( var i=0;i< arrTabs.length;i++){
             //identifier - HeaderText
            var tab3 = jtabs.addTab(arrTabs[i], arrTabs[i]);
            //activate the first tab
            if(!active){
                jtabs.activate(arrTabs[i]);
                active=true;
            }
       }
   
       this._tabArray=arrTabs;
       
    
       // Get the selected index from ClientState
        var state = this.get_ClientState();
        if (state !== null && state !== '') {
            var index = parseInt(state);
            if (index != this._selectedIndex) {
                this._selectedIndex = index;
                this._tab.activate(arrTabs[index]);
                this.raisePropertyChanged('SelectedIndex');
            }
        }
        this._tab.on('tabchange',this.onTabChange,this,true);
    },
    get_TabControl:function(){
        return this._tab;
    },
     get_SelectedIndex : function() {
        return this._selectedIndex;
    },
    set_SelectedIndex : function(value) {
        // Only make the change if they clicked a different pane

        if (this._selectedIndex != value) {
          
            this._selectedIndex = value;
            // Save the selected pane to preserve on postbacks
            this.set_ClientState(this._selectedIndex);
            this.raisePropertyChanged('SelectedIndex');
        }
    },
    onTabChange:function(tabPane,activePanel){
        //preserve selectedIndex for postbacks
        
        for(var i=0;i<  this._tabArray.length;i++){
            if(activePanel.id== this._tabArray[i]){
                this.set_SelectedIndex(i);
                break;
            }
        }
    }, 
    // Dispose of the TabControlBehavior
    dispose : function() {
        this._panes = null;
        //this._tab.purgeListeners();
        this._tabArray=null;
        GridControl.TabControlBehavior.callBaseMethod(this, 'dispose');
    }
    
   
 }

GridControl.TabControlBehavior.registerClass('GridControl.TabControlBehavior', AjaxControlToolkit.BehaviorBase);
